import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:terminal_flutter_demo/pages/getxpages/getX_home_page.dart';
import 'package:terminal_flutter_demo/pages/getxpages/getX_login_page.dart';
import 'package:terminal_flutter_demo/pages/getxpages/getX_shop_page.dart';
import 'package:terminal_flutter_demo/pages/getxpages/controllers/user.dart';
import 'package:terminal_flutter_demo/pages/getxpages/binding/shop.dart';
import 'package:terminal_flutter_demo/pages/getxpages/binding/user.dart';

class GetXRoutes {
  static final getPages = [
    GetPage(
      name: '/home',
      page: () => const GetXHomePage(),
    ),
    GetPage(
      name: '/shop',
      page: () => const GetXShopPage(),
      middlewares: [_CustomMiddleware()],
      bindings: [
        UserBinding(),
        ShopBinding(),
      ],
    ),
    GetPage(
      name: '/login',
      page: () => const GetXLoginPage(),
      binding: UserBinding(),
    ),
  ];
}

class _CustomMiddleware extends GetMiddleware {
  final userController = Get.put(UserController());

  @override
  RouteSettings? redirect(String? route) {
    // Get.lazyPut(() => UserController());
    // final UserController userController = Get.find();
    print('userController token: ${userController.token}');

    print('previousRoute: ${Get.previousRoute}');
    print('parameters: ${Get.parameters}');
    if (route == '/shop' && userController.token.isEmpty ||
        userController.user.value.token.isEmpty) {
      return const RouteSettings(name: '/login', arguments: {'id': 456});
    }
    return super.redirect(route);
  }
}
